package router

import (
	"github.com/gin-gonic/gin"
	"hound-sys/handler"
	"net/http"
)

func Init() *gin.Engine {
	// 1.创建路由
	r := gin.Default()
	r.Use(Cors())

	r.Static("/static", "./static")

	r.POST("/add", handler.Add) //登录接口
	r.POST("/ass", handler.Ass) //添加接口
	//开发用户和角色关联接口，完成用户和角色关联
	r.POST("/urole", handler.Urole)
	//实现角色权限查询接口，便于了解角色的权限数据
	r.POST("/rnode", handler.Rnode)
	//列表
	r.POST("/list", handler.List)

	return r
}
func Cors() gin.HandlerFunc {

	return func(c *gin.Context) {
		method := c.Request.Method
		origin := c.Request.Header.Get("Origin")
		if origin != "" {
			c.Header("Access-Control-Allow-Origin", "*")
			c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
			c.Header("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token, Authorization")
			c.Header("Access-Control-Allow-Credentials", "true")
			c.Set("content-type", "application/json")
		}
		//放行所有OPTIONS方法
		if method == "OPTIONS" {
			c.AbortWithStatus(http.StatusNoContent)
		}
		c.Next()
	}
}
